package Jets.ui
{
   import flash.display.DisplayObjectContainer;
   import flash.events.Event;
   import flash.text.TextField;
   import flash.text.TextFieldType;
   import flash.text.TextFormat;
   import Jets.display.JContainer;
   
   public class JInputText extends JContainer
   {
	   private var _password:Boolean = false;
	   private var _text:String = "";
	   protected var _tf:TextField;
	   
      public function JInputText(parent:DisplayObjectContainer=null,cx:Number=0,cy:Number=0,text:String="",change:Function=null) 
	  {
         this._text = text;
         super(parent,cx,cy);
         if(change != null)
         {
            addEventListener(Event.CHANGE,change);
         }
      }
      
      override protected function init() : void 
	  {
         super.init();
         setSize(100,20);
      }
      
      override protected function addChildren() : void 
	  {
         this._tf = new TextField();
         this._tf.type = TextFieldType.INPUT;
         this._tf.defaultTextFormat = new TextFormat("宋体",12,0);
         addChild(this._tf);
         this._tf.addEventListener(Event.CHANGE,this.onChange);
      }
      
      override public function draw() : void 
	  {
         super.draw();
         this._tf.displayAsPassword = this._password;
         this._tf.text = (this._text) || "";
         this._tf.width = _width - 2;
         if(this._tf.text == "")
         {
            this._tf.text = "X";
            this._tf.height = Math.min(this._tf.textHeight + 4,_height);
            this._tf.text = "";
         }
         else
         {
            this._tf.height = Math.min(this._tf.textHeight + 4,_height);
         }
         this._tf.x = 1;
         this._tf.y = Math.round(_height / 2 - this._tf.height / 2);
      }
      
      private function onChange(event:Event) : void 
	  {
         this._text = this._tf.text;
		 event.stopImmediatePropagation();
         this.dispatchEvent(new Event(Event.CHANGE));
      }
      
      public function get index() : int 
	  {
         return this._tf.selectionBeginIndex;
      }
      public function set index(value:int) : void 
	  {
         this._tf.setSelection(value,value);
      }
      
	  /**文本内容*/
      public function set text(value:String) : void 
	  {
         if(this._text != value)
         {
            this._text = value;
            this.draw();
         }
      }
      public function get text() : String 
	  {
         return this._text;
      }
      
	  /**与text 方法一样*/
      override public function get data() : Object 
	  {
         return this._text;
      }
      override public function set data(value:Object) : void 
	  {
         this.text = value + "";
      }
      
      public function set textColor(color:uint) : void {
         this._tf.textColor = color;
      }
      public function get textColor() : uint {
         return this._tf.textColor;
      }
      
	  /**过滤器 0-9 表示只能输入数字 a-z|A-Z表示只能输入字符*/
      public function set restrict(value:String) : void 
	  {
         this._tf.restrict = value;
      }
      public function get restrict() : String 
	  {
         return this._tf.restrict;
      }
      
	  /**最大能够输入的字符个数*/
      public function set maxChars(param1:int) : void 
	  {
         this._tf.maxChars = param1;
      }
      public function get maxChars() : int 
	  {
         return this._tf.maxChars;
      }
      
	  /**设置为密码输入*/
      public function set password(value:Boolean) : void 
	  {
         this._password = value;
         invalidate();
      }
      public function get password() : Boolean 
	  {
         return this._password;
      }
   }
}
